package com.itheima.many_to_many;

import com.itheima.bean.Classes;
import com.itheima.bean.Course;
import com.itheima.bean.Student;
import com.itheima.one_to_many.ClassesMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class Test01 {
    @Test
    public void selectAll() throws IOException {
        // 加载核心配置文件
        InputStream is = Resources.getResourceAsStream("MyBatisConfig.xml");

        // 获取SqlSession工厂对象
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);

        // 通过sqlSession工厂对象获取SqlSession对象
        SqlSession sqlSession = sqlSessionFactory.openSession();

        // 获取CardMapper接口的实现类对象
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);

        // 执行映射配置文件中的sql语句，并接收结果
        List<Student> list = mapper.selectAll();

        // 处理结果
        for (Student stu : list) {
            System.out.println(stu.getId()+","+stu.getName()+","+stu.getAge());
            List<Course> courses = stu.getCourss();
            for (Course cours : courses) {
                System.out.println("\t"+cours);
            }
        }
        // 释放资源
        sqlSession.close();
        is.close();
    }
}
